package com.wonders.hisdoctor.station.project.vaccine.service.impl;

import com.alibaba.fastjson.JSON;
import com.wonders.his.framework.dto.LoginUserDTO;
import com.wonders.his.framework.service.TokenService;
import com.wonders.his.framework.utils.oracle.IdUtils;
import com.wonders.his.framework.utils.oracle.OracleDateUtils;
import com.wonders.hisdoctor.station.constant.Constants;
import com.wonders.hisdoctor.station.enums.OracleSequenceEnum;
import com.wonders.hisdoctor.station.project.vaccine.domain.VaccineStock;
import com.wonders.hisdoctor.station.project.vaccine.service.IVaccineLogService;
import com.wonders.hisdoctor.station.project.vaccine.service.IVaccineStockService;
import com.wonders.hisdoctor.station.project.vaccine.mapper.VaccineStockMapper;
import com.wonders.hisdoctor.station.project.vaccine.vo.SetStatusVo;
import com.wonders.hisdoctor.station.project.vaccine.vo.VaccineStockRequestVo;
import com.wonders.hisdoctor.station.project.vaccine.vo.VaccineStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 疫苗库存表 服务类
 * </p>
 *
 * @author zhanghaonan
 * @since 2021/06/25
 */
@Service
public class VaccineStockServiceImpl implements IVaccineStockService {

    @Autowired
    private VaccineStockMapper vaccineStockMapper;

    @Autowired
    private IVaccineLogService vaccineLogService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IdUtils idUtils;

    @Autowired
    private OracleDateUtils oracleDateUtils;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(VaccineStock vo, HttpServletRequest request) {
        vo.setVaccineStockId(idUtils.getNextVal(OracleSequenceEnum.SEQ_ID_VACCINE_STOCK.getCode()).toString());
        //获取当前用户信息
        LoginUserDTO loginUserDTO = tokenService.getLoginUserDTO(tokenService.getToken(request));
        if (loginUserDTO != null && loginUserDTO.getSysSysuserVo() != null) {
            vo.setCreateId(loginUserDTO.getSysSysuserVo().getEmpNo());
            vo.setCreateName(loginUserDTO.getSysSysuserVo().getName());
        }
        vo.setCreateTime(oracleDateUtils.getOracleSysDate());
        int count = vaccineStockMapper.insert(vo);
        if (count > 0) {
            //记录日志
            vaccineLogService.saveLog(Constants.ADD, JSON.toJSONString(vo), request);
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(VaccineStock vo, HttpServletRequest request) {
        //获取当前用户信息
        LoginUserDTO loginUserDTO = tokenService.getLoginUserDTO(tokenService.getToken(request));
        if (loginUserDTO != null && loginUserDTO.getSysSysuserVo() != null) {
            vo.setUpdateId(loginUserDTO.getSysSysuserVo().getEmpNo());
        }
        vo.setUpdateTime(oracleDateUtils.getOracleSysDate());
        int count = vaccineStockMapper.updateById(vo);
        if (count > 0) {
            //记录日志
            vaccineLogService.saveLog(Constants.UPDATE, JSON.toJSONString(vo), request);
        }
        return count;
    }

    @Override
    public VaccineStock getById(VaccineStock vaccineStock) {
        return vaccineStockMapper.selectById(vaccineStock);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(VaccineStock vaccineStock, HttpServletRequest request) {
        VaccineStock logObj = vaccineStockMapper.selectById(vaccineStock);
        int count = vaccineStockMapper.deleteById(vaccineStock);
        if (count > 0) {
            //记录日志
            vaccineLogService.saveLog(Constants.DELETE, JSON.toJSONString(logObj), request);
        }
        return count;
    }

    @Override
    public List<VaccineStockVo> search(VaccineStockRequestVo vo) {
        return vaccineStockMapper.search(vo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int setStatus(SetStatusVo vo, HttpServletRequest request) {
        int count = vaccineStockMapper.setStatus(vo);
        if (count > 0) {
            //记录日志
            vaccineLogService.saveLog(Constants.UPDATE, JSON.toJSONString(vo), request);
        }
        return count;
    }
}
